﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jzy.UI.Web.Areas.Site.Models;
using Cms.BLL.Article;
using Cms.Entity.Article;
using Cms.Entity.Menu;
using Cms.BLL.Menu;


namespace Jzy.UI.Web.Areas.Site.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Site/Article/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(string menuCode, int currentPage, string menuType)
        {
            MenuBO menu = new MenuBO();

            ArticleListModel model = new ArticleListModel();
            model.MenuName = menu.GetInfo(menu.GetIDByCode(int.Parse(menuCode))).Name;
            model.PageSize = 12;
            model.CurrentPage = currentPage;
            model.MenuCode = menuCode;


            ArticleBO aritcle = new ArticleBO();
            IEnumerable<ArticleInfo> list = aritcle.GetDisplayListByCode(int.Parse(menuCode));
            model.RecordCount = list.Count();


            model.List = list.Skip((currentPage - 1) * model.PageSize).Take(model.PageSize);


            this.ViewData["index"] = menuCode;

            ViewResult result = View(model);

            if (!string.IsNullOrEmpty(menuType))
            {
                if ((MenuType)int.Parse(menuType) == MenuType.图片)
                {

                    result = View("PictureList", model);
                }
            }

            return result;

        }

        public ActionResult Detail(string menuCode, string id)
        {
            ArticleBO article = new ArticleBO();
            ArticleInfo info = null;


            if (!string.IsNullOrEmpty(menuCode))
            {
                IEnumerable<ArticleInfo> list = article.GetDisplayListByCode(int.Parse(menuCode), 1);
                if (list.Count() > 0)
                {
                    info = list.First();
                    article.SetDelegate(info);
                }
                else
                {
                    info = new ArticleInfo();
                    info.MenuInfo = (new MenuBO()).GetInfo(new MenuBO().GetIDByCode(int.Parse(menuCode)));
                }
            }
            else
            {
                info = article.GetInfo(id);
            }

            this.ViewData["index"] = info.MenuInfo.Code;
            this.ViewData["Description"] = info.Summary;
            this.ViewData["Title"] = this.ViewData["Keywords"] = info.KeyWords;

            return View(info);

        }

    }
}
